﻿Public Class clsJlMst
    Public Function getAllList(mst_value As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * FROM sf.t_mst_jl "
        szSql &= " WHERE 1 = 1 "
        If String.IsNullOrEmpty(mst_value) = False Then
            szSql &= " and mst_value like '%" + mst_value + "%'"
        End If
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        Return dt
    End Function

    Public Function getListByType(mst_type As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * FROM sf.t_mst_jl WHERE 1 = 1 and mst_type = ? "
        Return DBacs.ExecuteSql(szSql, -1, mst_type)
    End Function

    Public Function getListByType(mst_type As String, mst_value As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * FROM sf.t_mst_jl WHERE 1 = 1 and mst_type = ? and mst_value = ?"
        Return DBacs.ExecuteSql(szSql, -1, mst_type, mst_value)
    End Function

    Public Function getList() As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * FROM sf.t_mst_jl "
        szSql &= " WHERE 1 = 1 and validity = ?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, 1)
        Return dt
    End Function

    Public Function intoCk(code As String, en As entityjl_rk, usr As String) As Integer
        Dim sql As String = ""
        Dim rtn As Integer = 0
        DBacs.BeginTransaction()
        Try
            sql &= "INSERT INTO sf.b_jl_ck_in_details(ck_in_code, batch_no, mst_value, production_date, into_date, into_weight, location, update_time, assist_marker,validity) "
            sql &= "VALUES ( ?, ?, ?, ?, ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)), ?, ?)"
            With en
                rtn = DBacs.ExecuteUpdateSql(sql, -1, code, .batch_no, .mst_value, .production_date, .into_date, .into_weight, .location, .assist_marker, .validity)
            End With
            sql = ""
            sql &= "INSERT INTO sf.b_jl_ck_in( factory_id, ck_in_code, operator, update_time) "
            sql &= "VALUES ( ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "TS", code, usr)
            sql = ""
            sql &= " select * from sf.b_jl_ck where batch_no=? and mst_value= ? "
            Dim dt As DataTable = DBacs.ExecuteSql(sql, -1, en.batch_no, en.mst_value)
            sql = ""
            If dt.Rows.Count = 0 Then
                sql &= "INSERT INTO sf.b_jl_ck (batch_no, mst_value, cur_weight, update_time) "
                sql &= "VALUES ( ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
                DBacs.ExecuteUpdateSql(sql, -1, en.batch_no, en.mst_value, en.into_weight)
            ElseIf dt.Rows.Count = 1 Then
                sql &= "UPDATE sf.b_jl_ck  "
                sql &= " SET cur_weight = cur_weight + ?, update_time=(SELECT CURRENT_TIMESTAMP(6)) "
                sql &= " WHERE batch_no = ? and mst_value = ?"
                DBacs.ExecuteUpdateSql(sql, -1, en.into_weight, en.batch_no, en.mst_value)
            End If
            sql = ""
            sql &= "UPDATE sf.t_mst_jl "
            sql &= " SET cur_weight = cur_weight + ?, update_time=(SELECT CURRENT_TIMESTAMP(6)) "
            sql &= " WHERE mst_value = ? "
            rtn = DBacs.ExecuteUpdateSql(sql, -1, en.into_weight, en.mst_value)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

    Public Function outCk(code As String, en As entityjl_ck, usr As String) As Integer
        Dim sql As String = String.Empty
        Dim rtn As Integer = 0
        DBacs.BeginTransaction()
        Try
            sql &= "INSERT INTO sf.b_jl_ck_out_details(ck_out_code, prod_id, batch_no, mst_value, out_weight, xj_pgd_code, location, update_time) "
            sql &= "VALUES ( ?, ?, ?, ?, ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
            With en
                rtn = DBacs.ExecuteUpdateSql(sql, -1, code, .prod_id, .batch_no, .mst_value, .out_weight, .xj_pgd_code, .location)
            End With
            With en
                rtn = DBacs.ExecuteUpdateSql(sql, -1, code, .prod_id, .batch_no, .mst_value_bhj, .out_weight_bhj, .xj_pgd_code, .location)
            End With
            sql = String.Empty
            sql &= "INSERT INTO sf.b_jl_ck_out( factory_id, ck_out_code, operator, update_time) "
            sql &= "VALUES ( ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "TS", code, usr)
            sql = String.Empty
            sql &= "UPDATE sf.t_mst_jl "
            sql &= " SET cur_weight = cur_weight - ?, update_time=(SELECT CURRENT_TIMESTAMP(6)) "
            sql &= " WHERE mst_value = ? "
            rtn = DBacs.ExecuteUpdateSql(sql, -1, en.out_weight, en.mst_value)
            rtn = DBacs.ExecuteUpdateSql(sql, -1, en.out_weight_bhj, en.mst_value_bhj)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

End Class
